package com.qf.sy2103.ssm.controller;

import com.qf.sy2103.ssm.constants.Constants;
import com.qf.sy2103.ssm.entity.User;
import com.qf.sy2103.ssm.service.UserServcie;
import com.qf.sy2103.ssm.vo.MessageVo;
import com.qf.sy2103.ssm.vo.PageVo;
import com.qf.sy2103.ssm.vo.TableVo;
import com.qf.sy2103.ssm.vo.UserVo;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServcie userServcie;
    // http://localhost:8080/user/login
    @RequestMapping("/login")
    @ResponseBody
    public MessageVo  login(UserVo userVo,HttpServletRequest req,HttpSession session){
        User sname = userServcie.sname(userVo.getName());
        if (Objects.isNull(userVo.getName())){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_LOGINFAIL, Constants.USER_MSG_LOGINFAIL, null);
            return messageVo;
        }
        if (userVo.getYzm()!=null&&userVo.getYzm().equalsIgnoreCase((String) session.getAttribute("captcha"))){
            User login = userServcie.login(userVo.getName(), userVo.getPassword());
            if (Objects.nonNull(login)){
                session.setAttribute("userlogin",login);
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_LOGINSUCCESS, Constants.USER_MSG_LOGINSUCCESS, null);
                return messageVo;
            }
            else{
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_LOGINFAIL, Constants.USER_MSG_LOGINFAIL, null);
                return messageVo;
            }
        }
         MessageVo messageVo = new MessageVo(Constants.USER_CODE_CHAPCHARERROR, Constants.USER_MSG_CHAPCHARERROR, null);
        return messageVo;
    }
    // http://localhost:8080/user/regist'
    @RequestMapping("/regist")
    @ResponseBody
    public MessageVo Regist(UserVo userVo, MultipartFile src, HttpServletRequest req, HttpSession session) throws IOException {
        User sname = userServcie.sname(userVo.getName());
        String filename = src.getOriginalFilename();
        String unique = UUID.randomUUID().toString();
        String ext = FilenameUtils.getExtension(filename);
        String fileName = unique+"."+ext;
        String real_path = session.getServletContext().getRealPath("/upload_file");
        File file = new File(real_path);
        if (!file.exists()) {
            file.mkdirs();
        }
        src.transferTo(new File(real_path+"\\"+fileName));
        if (Objects.isNull(sname)){
            User user = new User(userVo.getName(), userVo.getPassword(),userVo.getEmail(),fileName,1);
            int regist = userServcie.regist(user);
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_REGISTSUCCESS, Constants.USER_MSG_REGISTSUCCESS, null);
            return messageVo;
        }
        else {
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_REGISTFAIL, Constants.USER_MSG_REGISTFAIL, null);
            return messageVo;
        }


    }
    @RequestMapping("/userAll")
    @ResponseBody
    public TableVo<User> userAll(PageVo pageVo,String name,Integer stu){
        User user = new User();
        user.setName(name);
        user.setStatus(stu);
        System.out.println(user);
        TableVo<User> users = userServcie.cha(pageVo,user);
        return users;
    }

    @RequestMapping("/add")
    @ResponseBody
    public MessageVo useradd(UserVo userVo){
        System.out.println(userVo);
        User sname = userServcie.sname(userVo.getName());
        if (Objects.isNull(sname)){
            if (userVo.getName()==null||userVo.getName()==""){
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDFAIL, Constants.USER_MSG_ADDFAIL, null);
                return messageVo;
            }
            else {
                int regist = userServcie.regist(new User(userVo.getName(), userVo.getPassword(), null, "c8468afa-0cc7-4cbd-8aef-1fb70033775e.jpg", 1));
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDSUCCESS, Constants.USER_MSG_ADDSUCCESS, null);
                return messageVo;
            }
        }
        else {
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDFAIL, Constants.USER_MSG_ADDFAIL, null);
            return messageVo;
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public MessageVo userupdate(UserVo userVo){
        System.out.println(userVo);
        User sname = userServcie.sname(userVo.getName());
        if (Objects.isNull(sname)){
            User user = new User(userVo.getName(), userVo.getPassword(), null, null, null);
            user.setId(userVo.getId());
            int edit = userServcie.edit(user);
            System.out.println(edit);
            if (edit==1){
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITSUCCESS, Constants.USER_MSG_EDITSUCCESS, null);
                return messageVo;
            }
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITFAIL, Constants.USER_MSG_EDITFAIL, null);
        return messageVo;
    }
    @RequestMapping("/del")
    @ResponseBody
    public MessageVo userdel(Integer[] ids){
        int del = userServcie.del(ids);
        if (del>0){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETESUCCESS, Constants.USER_MSG_DELETESUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETEFAIL, Constants.USER_MSG_DELETEFAIL, null);
        return messageVo;
    }
    // http://localhost:8080/user/select
    @RequestMapping("/select")
    public String SName(String name, HttpServletRequest req, Model model){
        User sname = userServcie.sname(name);
        if (Objects.nonNull(sname)){
           model.addAttribute("user",sname);
            return "forward:/cha.jsp";
        }
            req.setAttribute("msg","查无此人");
            return "forward:/cha.jsp";
    }
    @RequestMapping("/logout")
    public void logout(HttpSession session){
        session.setAttribute("userlogin",null);
    }

    @RequestMapping("/img")
    public void img(HttpServletResponse response,HttpSession session,String  filename) throws IOException {
        String realPath = session.getServletContext().getRealPath("/upload_file/" + filename);
        IOUtils.copy(new FileInputStream(new File(realPath)),response.getOutputStream());
    }
    @RequestMapping("/sname")
    @ResponseBody
    public User sname(HttpSession session){
        User user = (User)session.getAttribute("userlogin");
        User sname = userServcie.sname(user.getName());
        System.out.println(sname);
        return sname;
    }
    @RequestMapping("/export")
    @ResponseBody
    public MessageVo export(HttpSession session) throws IOException {


        final String realPath = session.getServletContext().getRealPath(Constants.FILE_UPLOAD_PATH);

        String filename = userServcie.exportAllDataBatch(realPath);


        return MessageVo.ok(Constants.USER_CODE_EXPORT_SUCCESS,Constants.USER_MSG_EXPORT_SUCCESS,filename);

    }

    @RequestMapping("/download")
    public void download(String filename, HttpSession session, HttpServletResponse response) throws IOException {


        String path = session.getServletContext().getRealPath(Constants.FILE_UPLOAD_PATH);

        String realFileName = path+"\\"+filename;

        response.setHeader("content-disposition","attachment;filename="+filename);


        IOUtils.copy(new FileInputStream(realFileName), response.getOutputStream());
    }

}
